#!/bin/sh
# PCP QA Test No. 1658
# Exercise /sys/module/zswap metrics.
#
# Copyright (c) 2023-2024 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check


test -d /sys/module || _notrun No support for sysfs module metrics
test -d /sys/module/zswap || _notrun No support for sysfs module zswap metric

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
    # end
}

_zswap_sysfs()
{
    parameter="$1"
    echo /sys/module/zswap/parameters/$parameter >> $seq_full
    $sudo cat /sys/module/zswap/parameters/$parameter | tee -a $seq_full \
	     | awk '{ print $NF }'
}

_zswap_pmprobe()
{
    parameter="$1"
    echo pmprobe -v sysfs.module.zswap.$parameter >> $seq_full
    pmprobe -v sysfs.module.zswap.$parameter | tee -a $seq_full \
	    | awk '{ print $NF }' | tr -d '"'
}

# real QA test starts here
pcp_value=`_zswap_pmprobe max_pool_percent`
sys_value=`_zswap_sysfs max_pool_percent`

echo "max_pool_percent" >> $seq_full
echo pcp_value=$pcp_value >> $seq_full
echo sys_value=$sys_value >> $seq_full

_within_tolerance "sysfs zswap pool value" $pcp_value $sys_value 0.05 -v
status=$?

pcp_value=`_zswap_pmprobe enabled`
sys_value=`_zswap_sysfs enabled`
echo "enabled" >> $seq_full
echo pcp_value=$pcp_value >> $seq_full
echo sys_value=$sys_value >> $seq_full

if [ "$pcp_value" = "$sys_value" ]; then
    echo "sysfs zswap enabled value correct"
else
    echo "sysfs zswap enabled mismatch:" $pcp_value "vs" $sys_value
    status=1  # fail o'clock
fi

# success, all done
exit
